package es.ats.codington.festival.servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

public class RegisterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("jsp/home.jsp");
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession sesion = request.getSession(true);
		
		if (sesion == null)
		{
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/");
	        requestDispatcher.forward(request, response);
		}
		else
		{
			Visitor visitor = new Visitor();
			VisitorService vs = new VisitorService();
		
			try {
					String firstname = request.getParameter("firstname");
					String lastname = request.getParameter("lastname");
					String email = request.getParameter("email");
					String phone = request.getParameter("phone");
					String nif = request.getParameter("nif");
					String address = request.getParameter("address");
					String username = request.getParameter("username");
					String password = request.getParameter("password");
		
					visitor.setFirstName(firstname);
					visitor.setLastName(lastname);			
					visitor.setEmail(email);
					visitor.setPhoneNumber(phone);
					visitor.setNif(nif);
					visitor.setAddress(address);			
					visitor.setUserName(username);
					visitor.setPassword(password);
					visitor.setAdmin(false);
		
					//Create a new visitor
					boolean r = vs.createVisitor(visitor);
					
					//If the visitor already exits
					if(r == false)
					{
						request.getRequestDispatcher("index.html").include(request, response);
						JOptionPane.showMessageDialog(null,"The user already exits. Please, try it again."); 
					}		
					//if the visitor don't exits
					else
					{
						int log=vs.login(visitor);
						if (log == 0)
						{
							RequestDispatcher requestDispatcher = request.getRequestDispatcher("index.html");
							requestDispatcher.forward(request, response);	
						}
						else
						{
							sesion.setAttribute("username", visitor.getUserName());
							sesion.setAttribute("peopleid", visitor.getPeopleId());
							sesion.setAttribute("isadmin", visitor.getAdmin());
							sesion.setAttribute("name", visitor.getFirstName() + " " + visitor.getLastName());
							sesion.setAttribute("email", visitor.getEmail());
						}
						
						RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/home.jsp");
						requestDispatcher.forward(request, response);			
					}
			}
			catch (Exception e) {
			request.setAttribute("error", "Something were wrong with your dates. Please, try it again in a few minits.");
			response.sendRedirect("jsp/error.jsp");
			}
		}
	}
	
}
